package extensions.android;

import java.util.Random;

import uclm.extensions.android.R;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

public class Element {

	private float _x;
	private float _y;
	private Bitmap _bitmap;
	private int _speedX, _speedY;
	
	public Element(Resources res, float x, float y)
	{
		Random rnd = new Random();
		_bitmap = BitmapFactory.decodeResource(res, R.drawable.icon);
		_x = x;
		_y = y;
		_speedX = rnd.nextInt(7) - 3;
		_speedY = rnd.nextInt(7) - 3;
	}
	
	public void tick(long dt)
	{
		_x += _speedX*dt/20;
		_y += _speedY*dt/20;
		checkBorders();
	}
	
	private void checkBorders()
	{
		if (_x < 0)
		{
			_speedX = -_speedX;
			_x = 0;
		}else if (_x+_bitmap.getWidth() >= Panel.WIDTH)
		{
			_speedX = -_speedX;
			_x = Panel.WIDTH - _bitmap.getWidth();
		}
		
		if (_y < 0)
		{
			_speedY = -_speedY;
			_y = 0;
		}else if (_y+_bitmap.getHeight() >= Panel.HEIGHT)
		{
			_speedY = -_speedY;
			_y = Panel.HEIGHT - _bitmap.getHeight();
		}
	}
	
	public float getX()
	{
		return _x;
	}
	
	public float getY()
	{
		return _y;
	}
	
	public void setPosition(float x, float y)
	{
		_x = x;
		_y = y;
	}
	
	public void doDraw(Canvas canvas)
	{
		canvas.drawBitmap(_bitmap, _x, _y, null);
	}
}
